package com.edgclub.edg.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.edgclub.edg.R;
import com.edgclub.edg.activity.GzTopicDetailActivity;
import com.edgclub.edg.activity.ImageViewPagerActivity;
import com.edgclub.edg.constant.BBConstant;
import com.edgclub.edg.model.PostsAlbum;
import com.edgclub.edg.utils.GlideUtil.GlideUtils;

import java.util.ArrayList;

/**
 */
public class BBImageShowAdapter extends BaseAdapter {
    private Context mContext;

    private int itemW;

    private ArrayList<PostsAlbum> postsAlbums;

    private GlideUtils glideUtils;

    private EditImageHolder holder;

    private int mTopicPosition;

    private int itemH = 0;

    private boolean isShowPlay = false;

    private PostsAlbum video;

    public BBImageShowAdapter(Context mContext, int mTopicPosition) {
        this.mContext = mContext;
        this.mTopicPosition = mTopicPosition;
        glideUtils = new GlideUtils();
    }

    public int getItemW() {
        return itemW;
    }

    public void setItemW(int itemW) {
        this.itemW = itemW;
    }

    public int getItemH() {
        return itemH;
    }

    public void setItemH(int itemH) {
        this.itemH = itemH;
    }

    public ArrayList<PostsAlbum> getPostsAlbums() {
        return postsAlbums;
    }

    public void setPostsAlbums(ArrayList<PostsAlbum> postsAlbums) {
        this.postsAlbums = postsAlbums;
    }

    @Override
    public int getCount() {
        if (null == postsAlbums) {
            return 0;
        }
        if(isShowPlay){
            return 1;
        }
        return Math.min(6, postsAlbums.size());

    }

    @Override
    public Object getItem(int arg0) {
        return null;
    }

    @Override
    public long getItemId(int arg0) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup arg2) {

        if (null == convertView) {
            convertView = LayoutInflater.from(mContext).inflate(
                    R.layout.item_bb_image, null);
            holder = new EditImageHolder();
            holder.image = (ImageView) convertView.findViewById(R.id.image);
            holder.play = (ImageView) convertView.findViewById(R.id.image_play);
            holder.tv_more = (TextView) convertView.findViewById(R.id.tv_more);
            convertView.setTag(holder);
        } else {
            holder = (EditImageHolder) convertView.getTag();
        }

        final PostsAlbum postsAlbum = postsAlbums.get(position);
        if (itemH == 0) {
            itemH = itemW;
        }
        ViewGroup.LayoutParams params = holder.image.getLayoutParams();
        params.width = itemW;
        params.height = itemH;
        holder.image.setLayoutParams(params);
        if (null != postsAlbum){
            glideUtils.Glidde_loadImage(mContext, postsAlbum.getThumbnail(), holder.image);
        }

        if(isShowPlay()){
            holder.play.setVisibility(View.VISIBLE);
            if(video!=null){
                glideUtils.Glidde_loadImage(mContext,video.getThumbnail(),holder.image);
            }
        }else{
            holder.play.setVisibility(View.GONE);
        }

        holder.tv_more.setVisibility(View.GONE);
        if(postsAlbums.size()>getCount() && position == (getCount()-1) && getCount()!=1){
            holder.tv_more.setVisibility(View.VISIBLE);
        }


        holder.image.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (null != video && video.getUrl() != null && video.getUrl().startsWith(BBConstant.URL_VIDEO_PROTOCOL)) {
//                    StringUtils.postToast(mContext,"视频处理");
//                    String id = postsAlbum.getUrl().replace("video://youku/", "");
//                    Intent intent = new Intent(mContext, GzYoukuPlayActivity.class);
//                    intent.putExtra("vid", id);
//                    mContext.startActivity(intent);
                    Intent intent = new Intent(mContext, GzTopicDetailActivity.class);
                    intent.putExtra("topicId", video.getPostsId() + "");
                    mContext.startActivity(intent);
                    ((Activity) mContext).overridePendingTransition(R.anim.slide_in_from_right,
                            R.anim.slide_out_to_left);

                } else {
                    Intent intent = new Intent(mContext,
                            ImageViewPagerActivity.class);
                    intent.putExtra("urls", postsAlbums);
                    intent.putExtra("position", position);
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    mContext.startActivity(intent);
                    ((Activity) mContext).overridePendingTransition(R.anim.head_in, R.anim.head_out);
                }
            }
        });

        return convertView;
    }

    public class EditImageHolder {

        public ImageView image,play;
        public TextView tv_more;
    }

    public boolean isShowPlay() {
        return isShowPlay;
    }

    public void setShowPlay(boolean showPlay,PostsAlbum video) {
        isShowPlay = showPlay;
        this.video = video;
    }

}
